package com.huluobo.a2307monthdemo.login

import android.app.AlertDialog
import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2307monthdemo.MainActivity
import com.huluobo.a2307monthdemo.databinding.ActivityLoginBinding
import com.huluobo.a2307monthdemo.register.RegisterActivity
import com.huluobo.lib_base.entity.UserBean
import com.huluobo.lib_base.mvvm.BaseActivity
import com.huluobo.lib_base.mvvm.MessageEvent
import com.huluobo.lib_base.net.Const
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {
    private var rememberPassword = false
    override fun initData() {
        vm.loginSuccess.observe(this) {
            ToastUtils.showLong("欢迎回来:${it.nickname}")
            SPUtils.getInstance().put(Const.PARAM_TOKEN, it.token)
            if (rememberPassword) {
                SPUtils.getInstance().put(Const.PARAM_USERNAME, it.username)
                SPUtils.getInstance().put(Const.PARAM_PASSWORD, it.password)
            } else {
                SPUtils.getInstance().remove(Const.PARAM_USERNAME)
                SPUtils.getInstance().remove(Const.PARAM_PASSWORD)
            }
            startAct(MainActivity::class.java)
        }
    }

    override fun initView() {
        //初始化需要判断用户
        if (SPUtils.getInstance().getString(Const.PARAM_USERNAME).isNotEmpty() && SPUtils.getInstance()
                .getString(Const.PARAM_PASSWORD).isNotEmpty()
        ) {
            binding.loginUsernameEt.text.insert(0, SPUtils.getInstance().getString(Const.PARAM_USERNAME))
            binding.loginPasswordEt.text.insert(0, SPUtils.getInstance().getString(Const.PARAM_PASSWORD))
            binding.loginRememberPassword.isChecked = true
            rememberPassword = true
        }
        binding.loginBtn.setOnClickListener {
            if (binding.loginContractCb.isChecked) {
                val username = binding.loginUsernameEt.text.toString()
                val password = binding.loginPasswordEt.text.toString()

                if (username.isNotEmpty() && password.isNotEmpty()) {
                    val map = mutableMapOf<String, String>()
                    map[Const.PARAM_USERNAME] = username
                    map[Const.PARAM_PASSWORD] = password
                    vm.login(map)
                } else {
                    binding.loginPasswordEt.error = "用户名密码不能为空"
                }
            } else {
                ToastUtils.showLong("请同意用户协议")
            }
        }


        binding.loginToRegister.setOnClickListener {
            startAct(RegisterActivity::class.java)
        }

        binding.loginRememberPassword.setOnCheckedChangeListener { buttonView, isChecked ->
            rememberPassword = isChecked
        }
        val contract = "同意《用户协议》"
        val builder = SpannableStringBuilder(contract)

        val blue = ForegroundColorSpan(Color.RED)
        builder.setSpan(blue, 2, contract.length, SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.loginContractTv.text = builder

        binding.loginContractTv.setOnClickListener {
            AlertDialog.Builder(this).setTitle("用户协议")
                .setMessage("用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议")
                .setPositiveButton("确认") { dialog, which ->
                    binding.loginContractCb.isChecked = true
                    dialog.dismiss()
                }.setNegativeButton("取消") { dialog, which ->
                    dialog.dismiss()
                }.show()
        }
    }

    override fun onEvent(messageEvent: MessageEvent) {
        super.onEvent(messageEvent)
        val userBean = Gson().fromJson(messageEvent.message, UserBean::class.java)
        binding.loginUsernameEt.text.insert(0, userBean.username)
        binding.loginPasswordEt.text.insert(0, userBean.password)
    }
}